﻿package base.loader.show {
	import flash.display.Sprite;
	import base.loader.ILoaderShow;
	import flash.display.MovieClip;
	import flash.events.Event;
	import com.greensock.TweenNano;
	
	public class LoaderShowSimple extends Sprite implements ILoaderShow {
		
		public var progress: LoaderProgress;
		public var labelDuration: MovieClip;
		
		private var numContentToLoad: uint;
		private var _numContentLoaded: uint;
		private var _countContentToLoad: uint;
		
		public function LoaderShowSimple(progress: LoaderProgress = null): void {
			this.numContentToLoad = 0;
			this.numContentLoaded = 0;
			this.countContentToLoad = 0;
			this.initProgress(progress);
			this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
		}
		
		private function initProgress(progress: LoaderProgress = null): void {
			if (this.progress == null) this.progress = progress;
			if (this.progress == null) {
				this.progress = new LoaderProgress();
				this.progress.x = this.progress.y = 0;	
			};
			if (this.progress != null) this.addChild(this.progress);	
		}

		private function onAddedToStage(e: Event): void {
			this.stage.addEventListener(Event.RESIZE, this.onStageResize);
			this.stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function onRemovedFromStage(e: Event): void {
			this.stage.removeEventListener(Event.RESIZE, this.onStageResize);
		}
		
		private function onStageResize(e: Event): void {
			this.x = (this.stage.stageWidth - this.width) / 2;
			this.y = (this.stage.stageHeight - this.height) / 2;
		}
		
		public function showToLoad(objLoadContent: Object): void {
			this.numContentToLoad++;
			this.progress.setLoadProgress(this.numContentToLoad / this.countContentToLoad);
		}
		
		public function showAllLoaded(): void {
			if (this.numContentLoaded == this.countContentToLoad) {
				this.progress.setLoadProgress(1);
			}
		}
		
		public function get numContentLoaded(): uint {
			return this._numContentLoaded;
		}
		
		public function set numContentLoaded(value: uint): void {
			this._numContentLoaded = value;
		}
		
		public function get countContentToLoad(): uint {
			return this._countContentToLoad;
		}
		
		public function set countContentToLoad(value: uint): void {
			this._countContentToLoad = value;
		}
		
		public function destroy(): void {
			if (this.progress != null) this.progress.destroy();
			if (this.stage != null) this.stage.removeEventListener(Event.RESIZE, this.onStageResize);
			this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
			TweenNano.killTweensOf(this);
		}
		
	}

}